﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DirectX.Direct3D;

namespace Client 
{
    public sealed class ClientEvent
    {

        public const int ZOMBIDIGUPDUSTBASE   = 420;
        public const int STONEFRAGMENTBASE    = 64;
        public const int HOLYCURTAINBASE      = 1390;
        public const int FIREBURNBASE         = 1630;
        public const int SCULPTUREFRAGMENT    = 1349;
        public const int HEARTPALPMARK        = 420;

        #region ClEvent - Kyndig - 07/08/2010

        public sealed class ClEvent
        {

            public int X;
            public int Y;
            public int Dir;
            public int px;
            public int py;
            public int EventTypeID;
            public int EventParam;
            public int ServerId;
            public Surface DrawSurface;
            public bool BoBlend;
            public int CalcFrameTime;
            public int CurFrame;
            public int Light;

            public void Create(int svid, int ax, int ay, int evtype)
            {
               ServerId = svid;
               X = ax;
               Y = ay;
               EventTypeID = evtype;
               EventParam = 0;
               BoBlend = false;
               CalcFrameTime = Environment.TickCount;
               CurFrame = 0;
               Light = 0;
            }

            public void Run()
            {
                DrawSurface = null;
                if (Environment.TickCount - CalcFrameTime > 80) 
                {
                    CalcFrameTime = Environment.TickCount;
                    CurFrame++;
                }
                
                switch (EventTypeID) 
                {
                    case Common.Global.ET_DIGOUTZOMBI:
                    {
                        // dsurface := frmMain.WMon6Img.GetCachedImage (ZOMBIDIGUPDUSTBASE+Dir, px, py);
                        break;
                    }

                    case Common.Global.ET_PILESTONES:
                    {         
                        if (EventParam <= 0) EventParam = 1;
                        if (EventParam > 5) EventParam = 5;
                        // dsurface := FrmMain.WEffectImg.GetCachedImage (STONEFRAGMENTBASE+(EventParam-1), px, py);
                        break;
                    }

                    case Common.Global.ET_HOLYCURTAIN:
                    {
                        // dsurface := FrmMain.WMagic.GetCachedImage (HOLYCURTAINBASE+(curframe mod 10), px, py);
                        BoBlend = true;
                        Light = 1;
                        break;
                    }

                    case Common.Global.ET_FIRE:
                    {
                        if (ClientGlobals.BoViewEffect) 
                        {
                            // dsurface := FrmMain.WMagic.GetCachedImage (FIREBURNBASE+((curframe div 2) mod 6), px, py);
                            BoBlend = true;
                            Light = 1;
                        }
                        break;
                    }

                    case Common.Global.ET_SCULPEICE:
                    {
                        // dsurface := FrmMain.WMon7Img.GetCachedImage (SCULPTUREFRAGMENT, px, py);
                        break;
                    }

                    case Common.Global.ET_HEARTPALP:
                    {
                        // dsurface := FrmMain.WMon14Img.GetCachedImage (HEARTPALPMARK, px, py);
                        break;
                    }
                }
            }
        }

        #endregion

        #region CLEventManager - Kyndig - 07/08/2010

        public sealed class CLEventManger
        {
            public List<ClEvent> EventList;

            public void Create()
            {
                EventList = new List<ClEvent>();
            }

            public void ClearEvents()
            {
                EventList.Clear();
            }

            public ClEvent AddEvent(ClEvent evn)
            {
                foreach (ClEvent tevn in EventList)
                {
                    if ((tevn == evn) || (tevn.ServerId == evn.ServerId))
                    {
                        return null;
                    }
                }
                EventList.Add(evn);
                return evn;
            }

            public void DelEvent(ClEvent evn)
            {
                foreach (ClEvent tevn in EventList)
                {
                    if (tevn == evn)
                    {
                        EventList.Remove(tevn);
                    }
                }
            }


            public void DelEventByID(int svid)
            {
                foreach (ClEvent tevn in EventList)
                {
                    if (tevn.ServerId == svid)
                    {
                        EventList.Remove(tevn);
                    }
                }
            }

            public ClEvent GetEvent(int ax, int ay, int etype)
            {
                foreach (ClEvent tevn in EventList)
                {
                    if ((tevn.X == ax) && (tevn.Y == ay) && (tevn.EventTypeID == etype))
                    {
                        return tevn;
                    }
                }
                return null;
            }

            public void Execute()
            {
                foreach (ClEvent tevn in EventList)
                {
                    tevn.Run();
                }
            }
        }

        #endregion
    }
}
